load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
load("@rules_rust//rust:defs.bzl", "rust_test")
load("//bazel:canisters.bzl", "rust_canister")
load("//bazel:defs.bzl", "rust_ic_test")

package(default_visibility = ["//visibility:public"])

DEPENDENCIES = [
    # Keep sorted.
    "//rs/rust_canisters/dfn_candid",
    "//rs/rust_canisters/dfn_core",
    "@crate_index//:build-info",
    "@crate_index//:candid",
]

MACRO_DEPENDENCIES = [
    # Keep sorted.
    "//rs/nervous_system/common/build_metadata",
]

BUILD_DEPENDENCIES = [
    # Keep sorted.
    "@crate_index//:build-info-build",
]

cargo_build_script(
    name = "build_script",
    srcs = ["build.rs"],
    build_script_env = {
        "CARGO_PKG_VERSION": "0.1.0",
        "CARGO_PKG_NAME": "ic-nervous-system-common-test-canister",
    },
    data = [],  # build script data (e.g. template files) goes here
    deps = BUILD_DEPENDENCIES,
)

rust_canister(
    name = "test_canister",
    srcs = glob(["src/**/*.rs"]),
    crate_name = "ic_nervous_system_common_test_canister",
    proc_macro_deps = MACRO_DEPENDENCIES,
    service_file = ":interface.did",
    version = "0.1.0",
    deps = DEPENDENCIES + [":build_script"],
)

rust_test(
    name = "canister_unit_test",
    crate = ":_wasm_test_canister",
    data = [":interface.did"],
    env = {"CARGO_MANIFEST_DIR": "rs/nervous_system/common/test_canister"},
    deps = ["@crate_index//:candid_parser"],
)

rust_ic_test(
    name = "integration_tests",
    srcs = ["tests/test.rs"],
    data = [
        ":test_canister",
    ],
    env = {
        "IC_NERVOUS_SYSTEM_COMMON_TEST_CANISTER_WASM_PATH": "$(rootpath :test_canister)",
    },
    deps = [
        # Keep sorted.
        "//rs/rust_canisters/canister_test",
        "//rs/state_machine_tests",
        "//rs/types/types",
        "@crate_index//:candid",
    ],
)
